generator client {
  provider = "prisma-client-js"
}

datasource db {
  provider = "mysql"
  url      = env("DATABASE_URL")
}

model User {
  id       Int    @id @default(autoincrement())
  name     String @unique
  password String
  rightId  Int

  products  Product[]
  right     Right       @relation(fields: [rightId], references: [id])
  userPlace UserPlace[]
  cabinets  Cabinet[]

  @@index([id])
  @@map("users")
}

model Right {
  id                 Int     @id @default(autoincrement())
  name               String
  placeActions       Boolean
  cabinetActions     Boolean
  productActions     Boolean
  userActions        Boolean
  rightActions       Boolean
  typeActions        Boolean
  consumablesActions Boolean
  orderSuccesing     Boolean
  creatingOrders     Boolean

  user User[]

  @@index([id])
  @@map("rights")
}

model UserPlace {
  id      Int @id @default(autoincrement())
  placeId Int
  userId  Int

  place Place @relation(fields: [placeId], references: [id], onDelete: Cascade)
  user  User  @relation(fields: [userId], references: [id], onDelete: Cascade)

  @@index([id])
  @@map("userplaces")
}

model Place {
  id   Int    @id @default(autoincrement())
  name String

  cabinets  Cabinet[]
  orders    Order[]
  userPlace UserPlace[]

  @@index([id])
  @@map("places")
}

model Cabinet {
  id            Int    @id @default(autoincrement())
  name          String
  placeId       Int
  responsibleId Int?

  place    Place     @relation(fields: [placeId], references: [id])
  users    User?     @relation(fields: [responsibleId], references: [id])
  products Product[]

  @@index([id])
  @@map("cabinets")
}

model Product {
  id              Int     @id @default(autoincrement())
  name            String  @db.Text
  description     String  @db.Text
  inventoryNumber String
  onUtil          Boolean @default(false)
  cabinetId       Int
  typeId          Int
  count           Int
  userAdded       Int

  user    User    @relation(fields: [userAdded], references: [id])
  type    Type    @relation(fields: [typeId], references: [id])
  cabinet Cabinet @relation(fields: [cabinetId], references: [id])

  @@index([id])
  @@map("products")
}

model Model {
  id     Int    @id @default(autoincrement())
  typeId Int
  name   String

  type        Type                @relation(fields: [typeId], references: [id], onDelete: Cascade)
  consumables ConsumableToModel[]

  @@index([id])
  @@map("models")
}

model ConsumableToModel {
  id           Int @id @default(autoincrement())
  modelId      Int
  consumableId Int

  model      Model      @relation(fields: [modelId], references: [id], onDelete: Cascade)
  consumable Consumable @relation(fields: [consumableId], references: [id], onDelete: Cascade)

  @@index([id])
  @@map("consumablesToModels")
}

model Consumable {
  id       Int     @id @default(autoincrement())
  name     String
  count    Int

  model     ConsumableToModel[]
  orderItem OrderItem[]

  @@index([id])
  @@map("consumables")
}

model OrderItem {
  id           Int @id @default(autoincrement())
  consumableId Int
  orderId      Int
  count        Int @default(1)

  order      Order      @relation(fields: [orderId], references: [id], onDelete: Cascade)
  consumable Consumable @relation(fields: [consumableId], references: [id], onDelete: Cascade)

  @@index([id])
  @@map("orderitems")
}

model Order {
  id        Int      @id @default(autoincrement())
  placeId   Int
  isDone    Boolean
  createdAt DateTime @default(now())

  orderItems OrderItem[]
  place      Place       @relation(fields: [placeId], references: [id], onDelete: Cascade)

  @@index([id])
  @@map("orders")
}

model Type {
  id   Int    @id @default(autoincrement())
  name String

  products Product[]
  models   Model[]

  @@index([id])
  @@map("types")
}
